home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 92 / CDMM92_1.ISO / SOF 2 SDK / sof2sdk-101.msi / _92D6AC311BB48EBA344BBABC89DA6AB0 / _A723ABD9001140A4B57FF14E5E335D4A < prev    next >
Encoding:
Text File  |  2002-06-05  |  8.8 KB  |  422 lines

  1. // Copyright (C) 2001-2002 Raven Software.
  2. //
  3. // cg_drawtools.c -- helper functions called by cg_draw, cg_scoreboard, cg_info, etc
  4. #include "cg_local.h"
  5.  
  6. /*
  7. ================
  8. CG_AdjustFrom640
  9.  
  10. Adjusted for resolution and screen aspect ratio
  11. ================
  12. */
  13. void CG_AdjustFrom640( float *x, float *y, float *w, float *h ) {
  14. #if 0
  15.     // adjust for wide screens
  16.     if ( cgs.glconfig.vidWidth * 480 > cgs.glconfig.vidHeight * 640 ) {
  17.         *x += 0.5 * ( cgs.glconfig.vidWidth - ( cgs.glconfig.vidHeight * 640 / 480 ) );
  18.     }
  19. #endif
  20.     // scale for screen sizes
  21.     *x *= cgs.screenXScale;
  22.     *y *= cgs.screenYScale;
  23.     *w *= cgs.screenXScale;
  24.     *h *= cgs.screenYScale;
  25. }
  26.  
  27. /*
  28. ================
  29. CG_FillRect
  30.  
  31. Coordinates are 640*480 virtual values
  32. =================
  33. */
  34. void CG_FillRect( float x, float y, float width, float height, const float *color ) {
  35.     trap_R_SetColor( color );
  36.  
  37.     CG_AdjustFrom640( &x, &y, &width, &height );
  38.     trap_R_DrawStretchPic( x, y, width, height, 0, 0, 0, 0, NULL, cgs.media.whiteShader );
  39.  
  40.     trap_R_SetColor( NULL );
  41. }
  42.  
  43. /*
  44. ================
  45. CG_DrawSides
  46.  
  47. Coords are virtual 640x480
  48. ================
  49. */
  50. void CG_DrawSides(float x, float y, float w, float h, float size) {
  51.     CG_AdjustFrom640( &x, &y, &w, &h );
  52.     size *= cgs.screenXScale;
  53.     trap_R_DrawStretchPic( x, y, size, h, 0, 0, 0, 0, NULL, cgs.media.whiteShader );
  54.     trap_R_DrawStretchPic( x + w - size, y, size, h, 0, 0, 0, 0, NULL, cgs.media.whiteShader );
  55. }
  56.  
  57. void CG_DrawTopBottom(float x, float y, float w, float h, float size) {
  58.     CG_AdjustFrom640( &x, &y, &w, &h );
  59.     size *= cgs.screenYScale;
  60.     trap_R_DrawStretchPic( x, y, w, size, 0, 0, 0, 0, NULL, cgs.media.whiteShader );
  61.     trap_R_DrawStretchPic( x, y + h - size, w, size, 0, 0, 0, 0, NULL, cgs.media.whiteShader );
  62. }
  63. /*
  64. ================
  65. UI_DrawRect
  66.  
  67. Coordinates are 640*480 virtual values
  68. =================
  69. */
  70. void CG_DrawRect( float x, float y, float width, float height, float size, const float *color ) {
  71.     trap_R_SetColor( color );
  72.  
  73.   CG_DrawTopBottom(x, y, width, height, size);
  74.   CG_DrawSides(x, y, width, height, size);
  75.  
  76.     trap_R_SetColor( NULL );
  77. }
  78.  
  79.  
  80.  
  81. /*
  82. ================
  83. CG_DrawPic
  84.  
  85. Coordinates are 640*480 virtual values
  86. =================
  87. */
  88. void CG_DrawPic( float x, float y, float width, float height, qhandle_t hShader ) {
  89.     CG_AdjustFrom640( &x, &y, &width, &height );
  90.     trap_R_DrawStretchPic( x, y, width, height, 0, 0, 1, 1, NULL, hShader );
  91. }
  92.  
  93.  
  94. /*
  95. ================
  96. CG_DrawStretchPic
  97.  
  98. Coordinates are 640*480 virtual values
  99. =================
  100. */
  101. void CG_DrawStretchPic( 
  102.     float x, 
  103.     float y, 
  104.     float width, 
  105.     float height, 
  106.     float sx, 
  107.     float sy, 
  108.     float sw, 
  109.     float sh, 
  110.     const float* color,
  111.     qhandle_t hShader 
  112.     ) 
  113. {
  114.     CG_AdjustFrom640( &x, &y, &width, &height );
  115.     trap_R_DrawStretchPic( x, y, width, height, sx, sy, sw, sh, NULL, hShader );
  116. }
  117.  
  118. /*
  119. ================
  120. CG_DrawRotatePic
  121.  
  122. Coordinates are 640*480 virtual values
  123. A width of 0 will draw with the original image width
  124. rotates around the upper right corner of the passed in point
  125. =================
  126. */
  127. void CG_DrawRotatePic( float x, float y, float width, float height,float angle, qhandle_t hShader ) {
  128.     CG_AdjustFrom640( &x, &y, &width, &height );
  129.     trap_R_DrawRotatePic( x, y, width, height, 0, 0, 1, 1, angle, hShader );
  130. }
  131.  
  132. /*
  133. ================
  134. CG_DrawRotatePic2
  135.  
  136. Coordinates are 640*480 virtual values
  137. A width of 0 will draw with the original image width
  138. Actually rotates around the center point of the passed in coordinates
  139. =================
  140. */
  141. void CG_DrawRotatePic2( float x, float y, float width, float height,float angle, qhandle_t hShader ) {
  142.     CG_AdjustFrom640( &x, &y, &width, &height );
  143.     trap_R_DrawRotatePic2( x, y, width, height, 0, 0, 1, 1, angle, hShader );
  144. }
  145.  
  146. /*
  147. =================
  148. CG_DrawStrlen
  149.  
  150. Returns character count, skiping color escape codes
  151. =================
  152. */
  153. int CG_DrawStrlen( const char *str ) {
  154.     const char *s = str;
  155.     int count = 0;
  156.  
  157.     while ( *s ) {
  158.         if ( Q_IsColorString( s ) ) {
  159.             s += 2;
  160.         } else {
  161.             count++;
  162.             s++;
  163.         }
  164.     }
  165.  
  166.     return count;
  167. }
  168.  
  169. /*
  170. =============
  171. CG_TileClearBox
  172.  
  173. This repeats a 64*64 tile graphic to fill the screen around a sized down
  174. refresh window.
  175. =============
  176. */
  177. static void CG_TileClearBox( int x, int y, int w, int h, qhandle_t hShader ) {
  178.     float    s1, t1, s2, t2;
  179.  
  180.     s1 = x/64.0;
  181.     t1 = y/64.0;
  182.     s2 = (x+w)/64.0;
  183.     t2 = (y+h)/64.0;
  184.     trap_R_DrawStretchPic( x, y, w, h, s1, t1, s2, t2, NULL, hShader );
  185. }
  186.  
  187.  
  188.  
  189. /*
  190. ==============
  191. CG_TileClear
  192.  
  193. Clear around a sized down screen
  194. ==============
  195. */
  196. void CG_TileClear( void ) 
  197. {
  198.     int        top, bottom, left, right;
  199.     int        w, h;
  200.  
  201.     w = cgs.glconfig.vidWidth;
  202.     h = cgs.glconfig.vidHeight;
  203.  
  204.     if ( cg.refdef.x == 0 && cg.refdef.y == 0 && 
  205.         cg.refdef.width == w && cg.refdef.height == h ) {
  206.         return;        // full screen rendering
  207.     }
  208.  
  209.     top = cg.refdef.y;
  210.     bottom = top + cg.refdef.height-1;
  211.     left = cg.refdef.x;
  212.     right = left + cg.refdef.width-1;
  213.  
  214.     // clear above view screen
  215.     CG_TileClearBox( 0, 0, w, top, cgs.media.backTileShader );
  216.  
  217.     // clear below view screen
  218.     CG_TileClearBox( 0, bottom, w, h - bottom, cgs.media.backTileShader );
  219.  
  220.     // clear left of view screen
  221.     CG_TileClearBox( 0, top, left, bottom - top + 1, cgs.media.backTileShader );
  222.  
  223.     // clear right of view screen
  224.     CG_TileClearBox( right, top, w - right, bottom - top + 1, cgs.media.backTileShader );
  225. }
  226.  
  227.  
  228.  
  229. /*
  230. ================
  231. CG_FadeColor
  232. ================
  233. */
  234. float *CG_FadeColor( int startMsec, int totalMsec ) {
  235.     static vec4_t        color;
  236.     int            t;
  237.  
  238.     if ( startMsec == 0 ) {
  239.         return NULL;
  240.     }
  241.  
  242.     t = cg.time - startMsec;
  243.  
  244.     if ( t >= totalMsec ) {
  245.         return NULL;
  246.     }
  247.  
  248.     // fade out
  249.     if ( totalMsec - t < FADE_TIME ) {
  250.         color[3] = ( totalMsec - t ) * 1.0/FADE_TIME;
  251.     } else {
  252.         color[3] = 1.0;
  253.     }
  254.     color[0] = color[1] = color[2] = 1;
  255.  
  256.     return color;
  257. }
  258.  
  259.  
  260. /*
  261. ================
  262. CG_TeamColor
  263. ================
  264. */
  265. float *CG_TeamColor( int team ) {
  266.     static vec4_t    red = {1, 0.2f, 0.2f, 1};
  267.     static vec4_t    blue = {0.2f, 0.2f, 1, 1};
  268.     static vec4_t    other = {1, 1, 1, 1};
  269.     static vec4_t    spectator = {0.7f, 0.7f, 0.7f, 1};
  270.  
  271.     switch ( team ) {
  272.     case TEAM_RED:
  273.         return red;
  274.     case TEAM_BLUE:
  275.         return blue;
  276.     case TEAM_SPECTATOR:
  277.         return spectator;
  278.     default:
  279.         return other;
  280.     }
  281. }
  282.  
  283.  
  284. /*
  285. =================
  286. CG_GetColorForHealth
  287. =================
  288. */
  289. void CG_GetColorForHealth( vec4_t color, int health, int armor ) 
  290. {
  291.     int                count;
  292.     int                max;
  293.  
  294.     VectorCopy ( colorWhite, color );
  295.     
  296.     color[3] = 1.0f;
  297.  
  298.     // calculate the total points of damage that can
  299.     // be sustained at the current health / armor level
  300.     if ( health <= 0 ) 
  301.     {
  302.         VectorCopy ( colorBlack, color );
  303.         return;
  304.     }
  305.  
  306.     count = armor;
  307.     max = health * ARMOR_PROTECTION / ( 1.0 - ARMOR_PROTECTION );
  308.  
  309.     if ( max < count ) 
  310.     {
  311.         count = max;
  312.     }
  313.  
  314.     health += count;
  315.  
  316.     // set the color based on health
  317.     color[0] = 1.0f;
  318.     if ( health >= 100 ) 
  319.     {
  320.         color[2] = 1.0f;
  321.     } 
  322.     else if ( health < 66 ) 
  323.     {
  324.         color[2] = 0;
  325.     } 
  326.     else 
  327.     {
  328.         color[2] = ( health - 66 ) / 33.0;
  329.     }
  330.  
  331.     if ( health > 60 ) 
  332.     {
  333.         color[1] = 1.0f;
  334.     } 
  335.     else if ( health < 30 ) 
  336.     {
  337.         color[1] = 0;
  338.     } 
  339.     else 
  340.     {
  341.         color[1] = ( health - 30 ) / 30.0;
  342.     }
  343. }
  344.  
  345. /*
  346. =====================
  347. CG_DrawText
  348.  
  349. Renders text on the screen
  350. =====================
  351. */
  352. void CG_DrawText ( float x, float y, qhandle_t font, float scale, vec4_t color, const char* text, int limit, int flags )
  353. {
  354.     x      *= cgs.screenXScale;
  355.     y      *= cgs.screenYScale;
  356.     scale *= (cgs.screenXScale);
  357.  
  358.     trap_R_DrawText ( (int)x, (int)y, font, scale, color, text, limit, flags );
  359. }
  360.  
  361. /*
  362. =====================
  363. CG_DrawTimer
  364.  
  365. Draws a timer on the screen with the given parameters
  366. =====================
  367. */
  368. void CG_DrawTimer ( float x, float y, qhandle_t font, float scale, vec4_t color, int flags, int msec )
  369. {
  370.     const char* s;
  371.  
  372.     if ( msec )
  373.     {
  374.         int         mins;
  375.         int         seconds;
  376.         int         tens;
  377.         qboolean neg;
  378.  
  379.         // Remember the milliseconds were negative
  380.         // before making them positive again
  381.         if ( msec < 0 )
  382.         {
  383.             msec *= -1;
  384.             neg  = qtrue;
  385.         }
  386.         else
  387.         {
  388.             neg = qfalse;
  389.         }
  390.  
  391.         seconds = msec / 1000;
  392.         mins = seconds / 60;
  393.         seconds -= mins * 60;
  394.         tens = seconds / 10;
  395.         seconds -= tens * 10;
  396.  
  397.         s = va( "%s%i:%i%i", neg?"-":"",mins, tens, seconds );
  398.     }
  399.     else
  400.     {
  401.         s = "------";
  402.     }
  403.  
  404.     CG_DrawText ( x, y, font, scale, color, s, 0, flags );
  405. }
  406.  
  407. /*
  408. =====================
  409. CG_DrawTextWithCursor
  410.  
  411. Renders text on the screen with a blinking cursor
  412. =====================
  413. */
  414. void CG_DrawTextWithCursor ( float x, float y, qhandle_t font, float scale, vec4_t color, const char* text, int limit, int flags, int cursorPos, char cursor )
  415. {
  416.     x      *= cgs.screenXScale;
  417.     y      *= cgs.screenYScale;
  418.     scale *= (cgs.screenXScale);
  419.  
  420.     trap_R_DrawTextWithCursor ( (int)x, (int)y, font, scale, color, text, limit, flags, cursorPos, cursor );
  421. }
  422.